package cz.hanis.android.geography.activities;

import java.util.ArrayList;
import java.util.List;

import cz.hanis.android.geography.R;
import cz.hanis.android.geography.TestActivity;
import cz.hanis.android.geography.WrongResourceException;
import cz.hanis.android.geography.models.Country;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class FlagCountryTestActivity extends TestActivity {

	private static final String TAG = "FlagCountryTestActivity";
	private ImageView ivFlagImage;

	private String questionId;
	private int correct;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTestLayout(R.layout.flag_country_test);
		setQuestion(getString(R.string.tv_flag_country_question));
		ivFlagImage = (ImageView) findViewById(R.id.iv_flag_country_flag);
		setQuestionView(ivFlagImage);
		addAnswerView((Button) findViewById(R.id.bt_flag_country_country1));
		addAnswerView((Button) findViewById(R.id.bt_flag_country_country2));
		addAnswerView((Button) findViewById(R.id.bt_flag_country_country3));
		//addAnswerView((Button) findViewById(R.id.bt_flag_country_country4));
		setListeners();
		if (savedInstanceState == null) {
			newRound();
		} else {
			try {
				questionId = savedInstanceState.getString(TestActivity.QUESTION_KEY);
				ivFlagImage.setImageDrawable(getGeogrephyApplication().getFlagDrawable(questionId));
			} catch (WrongResourceException e) {
				Log.d(FlagCountryTestActivity.TAG, e.getMessage());
		    }					
			correct = savedInstanceState.getInt(TestActivity.CORRECT_POSITION_KEY);
		    ArrayList<String> answerList = savedInstanceState.getStringArrayList(ANSWER_LIST_KEY);	    
			for(int i = 0; i < getNumberOfAnswers(); i++) {
				Button button = (Button) getAnswerViews().get(i);
				button.setText(answerList.get(i));
			}			
			restoreRound();
		}		
		
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	    outState.putInt(CORRECT_POSITION_KEY, correct);
	    outState.putString(QUESTION_KEY, questionId);	    	    
	    ArrayList<String> answerList = new ArrayList<String>();	    
		for(int i = 0; i < getNumberOfAnswers(); i++) {
			Button button = (Button) getAnswerViews().get(i);
			answerList.add(button.getText().toString());
		}
		outState.putStringArrayList(ANSWER_LIST_KEY, answerList);	    
	}		
	

	@Override
	protected int onAnswer(int answer) {		
		int result = TestActivity.ANSWERE_NONE;
		setBackgroudButton(correct, R.drawable.button_9patch_green);
		
		if (answer == correct) {
			result = TestActivity.ANSWERE_CORRECT;			
		} else if (answer == TestActivity.ANSWERE_TIMED_OUT) {
			result = TestActivity.ANSWERE_TIMED_OUT;
		} else {
			setBackgroudButton(answer, R.drawable.button_9patch_red);
			result = TestActivity.ANSWERE_INCORRECT;
		}
		return result;
	}
	

	@Override
	protected void onNewRound() {
		List<Country> randomCountries = getTools().getRandomCountries(
				getNumberOfAnswers(), getTest().getContinent());
		correct = getTools().getRandom(getNumberOfAnswers());
		try {
			questionId = randomCountries.get(correct).getId();
			ivFlagImage.setImageDrawable(getGeogrephyApplication().getFlagDrawable(questionId));			
		} catch (WrongResourceException e) {
			Log.d(FlagCountryTestActivity.TAG, e.getMessage());
	    }		
		
		for(int i = 0; i < getNumberOfAnswers(); i++) {
			setBackgroudButton(i, R.drawable.button_9patch);
			Button button = (Button) getAnswerViews().get(i);
			button.setText(randomCountries.get(i).getName());
		}
	}

	@Override
	protected void onRestoreRound() {
		// TODO Auto-generated method stub
		
	}

}